package com.ymh.git.thread.juc.account;

import com.ymh.git.thread.juc.SleepTools;

/**
 * Created by aivin on 2018/11/1.
 * <p>
 * 不安全的转账动作的实现
 */
public class TransferAccount implements ITransfer {


    @Override
    public void transfer(UserAccount from, UserAccount to, int amount) {
        synchronized (from) { //先锁转出账户
            System.out.println(Thread.currentThread().getName() + " get" + from);
            SleepTools.ms(100);
            synchronized (to) { //在锁转入账户
                System.out.println(Thread.currentThread().getName() + " get " + to);
                from.outMoney(amount);
                to.inMoney(amount);
            }
        }
    }


}
